#include "VFVErrorStatic.h"

registerMooseObject("MooseApp", VFVErrorStatic);

template <>
InputParameters validParams<VFVErrorStatic>()
{
	InputParameters params = validParams<ElementIntegralPostprocessor>();
	params.addRequiredCoupledVar("variable", "The name of the variable that this object operates on");
	return params;
}

VFVErrorStatic::VFVErrorStatic(const InputParameters& parameters) :
	ElementIntegralPostprocessor(parameters),
	MooseVariableInterface<Real>(this,
		false,
		"variable",
		Moose::VarKindType::VAR_ANY,
		Moose::VarFieldType::VAR_FIELD_STANDARD),
	VFVInterface(*getCheckedPointerParam<VFVProblem*>("_vfv_problem"), _assembly),
	_u(coupledValue("variable")),
	_grad_u(coupledGradient("variable"))
{
	addMooseVariableDependency(mooseVariable());
}

Real VFVErrorStatic::computeIntegral()
{
	Real sum = 0;
	Real average = 0;
	for (_qp = 0; _qp < _qrule->n_points(); _qp++)
	{
		Real x = _q_point[_qp](0);
		Real exact_value = sin(2 * M_PI * (x - _t));
		average += _JxW[_qp] * _coord[_qp] * exact_value;
	}
	average /= _current_elem_volume;

	for (_qp = 0; _qp < _qrule->n_points(); _qp++)
	{
		Real x = _q_point[_qp](0);
		Real exact_value = sin(2 * M_PI * (x - _t));
		sum += _JxW[_qp]  * std::fabs(_u_rs[_qp]- exact_value);
	}

	return sum;
}

Real VFVErrorStatic::computeQpIntegral()
{
	Real x = _q_point[_qp](0);
	Real exact_value = sin(2 * M_PI * (x - _t));

	Real error = std::fabs(exact_value-_u_rs[_qp]);

	return error;
}